package com.wfm.leetcode.editor.cn;

import com.wfm.common.TreeNode;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class POne02二叉树的层序遍历{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();

        if(root==null) return res;

        Deque<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()){
            List<Integer> path = new ArrayList<>();
            int curSize = q.size();
            for (int i = 0; i < curSize; i++) {
                TreeNode poll = q.poll();
                path.add(poll.val);
                if(poll.left!=null) q.offer(poll.left);
                if(poll.right!=null) q.offer(poll.right);
            }
            res.add(path);
        }
        return res;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new POne02二叉树的层序遍历().new Solution();
    }
}